using System;

namespace MT5Trade.Models.MarketData
{
    /// <summary>
    /// 市场数据测试查询过滤器
    /// 严格遵守 generic-rule.md 规则
    /// 用于从数据库查询真实测试数据
    /// </summary>
    public class MarketDataTestFilter
    {
        public string Symbol { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public string IntervalType { get; set; }
        public string DataSource { get; set; }
        public int? Limit { get; set; }
        public int? Offset { get; set; }

        /// <summary>
        /// 构建SQL WHERE子句
        /// </summary>
        public string BuildWhereClause()
        {
            var conditions = new System.Collections.Generic.List<string>();

            if (!string.IsNullOrEmpty(Symbol))
                conditions.Add($"symbol = '{Symbol}'");

            if (StartTime.HasValue)
                conditions.Add($"time >= '{StartTime.Value:yyyy-MM-dd HH:mm:ss}'");

            if (EndTime.HasValue)
                conditions.Add($"time <= '{EndTime.Value:yyyy-MM-dd HH:mm:ss}'");

            if (!string.IsNullOrEmpty(IntervalType))
                conditions.Add($"interval_type = '{IntervalType}'");

            if (!string.IsNullOrEmpty(DataSource))
                conditions.Add($"data_source = '{DataSource}'");

            return conditions.Count > 0
                ? "WHERE " + string.Join(" AND ", conditions)
                : "";
        }

        /// <summary>
        /// 构建完整的SQL查询
        /// </summary>
        public string BuildQuery()
        {
            var query = $"SELECT * FROM market_data_test {BuildWhereClause()} ORDER BY time ASC";

            if (Limit.HasValue)
            {
                query += $" LIMIT {Limit.Value}";
                if (Offset.HasValue)
                {
                    query += $" OFFSET {Offset.Value}";
                }
            }

            return query;
        }
    }
}